﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
// add
using System.IO;
using System.Data;
using FormBaseLib;

namespace FormBase
{
    public class CProjectRun: CBase
    {
        DataSetProjectRun mDataSet;
        bool mbNewRow;
        public string msFile { get; private set; }
        DataSetProjectRun.TProjectRunDataTable mTable;
        public DataSetProjectRun.TProjectRunRow mRow { get; set; }
        public CProjectRun()
        {
            mDataSet = new DataSetProjectRun();
            mbNewRow = false;
        }

        public bool Save()
        {
            if (string.IsNullOrEmpty(msFile))
            {
                SetLastError("ERROR: Empty filename.");
                return false;
            }
            if (!Directory.Exists(Path.GetDirectoryName(msFile)))
            {
                SetLastError(string.Format("ERROR: Directory does not exist in file {0}.", msFile));
                return false;
            }

            // Project info:
            DateTime dNow = DateTime.Now;
            mRow.FCompanyID = CProject.mApp.msCompanyID;
            mRow.FSystemID = CProject.mApp.msSystemID;
            mRow.FProjectID = CProject.mApp.msProjectID;
            mRow.FVersionAssemply = CProject.mApp.msAssemblyVersion;
            mRow.FVersionApp = CProject.mApp.msAssemblyVersion;
            mRow.FEnvironment = (int)CProject.mEnvironment;
            mRow.FPathRoot = CProject.mApp.msPathRoot;
            mRow.FPathLast = CProject.mApp.msPathLastUsed;
            mRow.FTimeRelease = CProject.mApp.mTimeRelease;
            mRow.FTimeStart = CProject.mApp.mTimeStart;
            mRow.FTimeEnd = dNow;
            //rowData.FCounter = 0;
            //rowData.FTimeResetCounter = DateTime.Now;
            mRow.FLeft = Program.gFMain.Left;
            mRow.FTop = Program.gFMain.Top;
            mRow.FWidth = Program.gFMain.Width;
            mRow.FHeight = Program.gFMain.Height;
            //rowData.FCreateTime = DateTime.Now;
            mRow.FUpdateTime = dNow;

            File.WriteAllText(msFile, mDataSet.GetXml(), Encoding.UTF8);
            return true;
        }
        public bool SaveSchema(string sFile)
        {
            File.WriteAllText(sFile, mDataSet.GetXmlSchema().Replace("utf-16", "utf-8"), Encoding.UTF8);
            return true;
        }

        public bool Load(string sFile)
        {
            long l1;
            DateTime d1;

            msFile = sFile;
            ClearLastError();
            if (string.IsNullOrWhiteSpace(msFile))
            {
                SetLastError(string.Format("ERROR: Empty file anme."));
                return false;
            }
            if (!File.Exists(msFile))
            {
                SetLastError(string.Format("ERROR: File does NOT exist. {0}.", msFile));
                return false;
            }
            mDataSet.Clear();

            XmlReadMode mode1 = mDataSet.ReadXml(msFile);
            //todo if (mode1 == XmlReadMode.Auto)
//            DataSetProjectRun.TProjectRunDataTable t1;
            mTable = mDataSet.TProjectRun;
            if (mTable.Rows.Count < 1)
            {
                SetLastError(String.Format("ERROR: Rows count < 1 in file {0}.", msFile));
                return false;
            }

            mRow = mTable.Rows[0] as DataSetProjectRun.TProjectRunRow;

            // if null fields
            if (!mRow.IsFPathLastNull())
                CProject.mApp.msPathLastUsed = mRow.FPathLast;

            // read back last counter and reset datetime
            l1 = CProject.mApp.mlCounter;
            d1 = CProject.mApp.mTimeResetCounter;
            if (!mRow.IsFCounterNull())
                l1 = mRow.FCounter;

            if (!mRow.IsFTimeResetCounterNull())
                d1 = mRow.FTimeResetCounter;

            CProject.mApp.SetCurrentCounter(l1, d1);

            mDataSet.AcceptChanges(); // make no changes!
            return true;
        }
        public void CreateNewRowDefault()
        {
            DataSetProjectRun.TProjectRunDataTable t1;
            mDataSet.Clear();
            t1 = mDataSet.TProjectRun;

            // default values, all fields MUST be included.
            DateTime dNow = DateTime.Now;

            mRow = t1.NewTProjectRunRow();
            mRow.FID = CProject.mApp.msProjectID;
            mRow.FCompanyID = CProject.mApp.msCompanyID;
            mRow.FSystemID = CProject.mApp.msSystemID;
            mRow.FProjectID = CProject.mApp.msProjectID;
            mRow.FVersionAssemply = CProject.mApp.msAssemblyVersion;
            mRow.FVersionApp = CProject.mApp.msAssemblyVersion;
            mRow.FEnvironment = (int) CProject.mEnvironment;
            mRow.FPathRoot = CProject.mApp.msPathRoot;
            mRow.FPathLast = CProject.mApp.msPathLastUsed;
            mRow.FTimeRelease = CProject.mApp.mTimeRelease;
            mRow.FTimeStart = CProject.mApp.mTimeStart;
            mRow.FTimeEnd = dNow;
            mRow.FCounter = 0;
            mRow.FTimeResetCounter = dNow;
            //rowData.FLeft = Program.gFMain.Left;
            //rowData.FTop = Program.gFMain.Top;
            //rowData.FWidth = Program.gFMain.Width;
            //rowData.FHeight = Program.gFMain.Height;
            mRow.FLeft = 0;
            mRow.FTop = 0;
            mRow.FWidth = 640;
            mRow.FHeight = 480;
            mRow.FCreateTime = dNow;
            mRow.FUpdateTime = dNow;
            t1.AddTProjectRunRow(mRow);

            mbNewRow = true;
        }

        public bool HasChanges()
        {
            if (mbNewRow)
                return true;

            return mDataSet.HasChanges();
        }

    }
}
